<?php
/**
 * --------------------------------------------------------------
 * 权限规则控制器
 * --------------------------------------------------------------
 * Light Year Doc
 * @author yinq<3331653644@qq.com>
 * --------------------------------------------------------------
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\admin\model\AuthRule as AuthRuleModel;
use util\Tree;

class AuthRule extends AdminBase
{
    /**
     * 权限列表
     */
    public function index()
    {
        $map  = $this->getSimpleSearchMap();
        $list = AuthRuleModel::where($map['where'])
                ->order('id desc')
                ->paginate(config('app.site_setting.list_rows'), false, [
                    'query' => $map['conditions']
                ]);

        $this->assign([
            'list'         => $list,
            'keyword'      => $map['conditions']['keyword'],
            'search_field' => $map['conditions']['search_field'],
            'title'        => '权限规则列表'
        ]);
        return $this->fetch();
    }

    /**
     * 添加规则
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->post();

            $result = $this->validate($data, 'AuthRule');
            if (true !== $result) $this->error($result);

            if (AuthRuleModel::create($data)) {
                return $this->success('新增成功', url('index'));
            } else {
                return $this->error('新增失败');
            }
        } else {
            $smenu = AuthRuleModel::queryOneOrList(['status' => 1], 'id,title,type,pid', 'list');
            $smenu = Tree::toFormatTree($smenu);
            $smenu = array_merge([0 => ['id' => 0, 'title_show' => '顶级规则']], $smenu);

            $this->assign([
                'smenu' => $smenu,
                'title' => '新增规则'
            ]);
            return $this->fetch();
        }
    }
    
    /**
     * 编辑规则
     */
    public function edit()
    {
        $id = input('id/d');
        if ($id < 0 || !is_numeric($id)) $this->error('参数错误');
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $result = $this->validate($data, 'AuthRule');
            if (true !== $result) $this->error($result);

            if (AuthRuleModel::update($data)) {
                return $this->success('编辑成功', url('index'));
            } else {
                return $this->error('编辑失败');
            }
        } else {
            $smenu = AuthRuleModel::queryOneOrList(['status' => 1], 'id,title,type,pid', 'list');
            $smenu = Tree::toFormatTree($smenu);
            $smenu = array_merge([0 => ['id' => 0, 'title_show' => '顶级规则']], $smenu);
            $info  = AuthRuleModel::get($id);

            $this->assign([
                'info'  => $info,
                'smenu' => $smenu,
                'title' => '编辑规则'
            ]);
    		return $this->fetch();
        }
    }

    /**
     * 删除规则
     */
    public function delete($ids = 0)
    {
        (int) $ids || $this->error('参数错误');
        $map = ['id' => $ids];

        $result = AuthRuleModel::where($map)->delete();
        return $result ? $this->success('删除成功') : $this->error('删除失败');
    }
	
	/**
	 * 禁用/启用
	 * @param array $ids id数组
     * @param integer $status 状态值
     */
    public function setStatus($ids = [], $status)
    {
        empty($ids) && $this->error('请选择要操作的数据');
        !is_numeric((int) $status) && $this->error('参数错误');
        
        $map  = ['id' => $ids];
        $info = AuthRuleModel::where($map)->setField('status', $status);
        $tips = $status == 1 ? '启用' : '禁用';
        return $info !== FALSE ? $this->success($tips.'成功') : $this->error($tips.'失败');
    }
}